package com.xinsoft.controller.mes;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesBomDTO;
import com.xinsoft.entity.dto.MesBomPageParam;
import com.xinsoft.entity.po.MesBom;
import com.xinsoft.service.MesBomService;
import com.xinsoft.service.MesBomService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/mes/bom")
public class MesBomController {

    @Autowired
    private MesBomService mesBomService;

    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/add")
    public Response<?> add(@RequestBody MesBomDTO mesBom) {
        // 参数校验
        ValidatorUtils.validateEntity(mesBom);
        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.MES_BOM_DOCUMENT);
        return Response.succeed(mesBomService.addMesBom(mesBom));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  MesBomDTO mesBom) {
        // 参数校验
        ValidatorUtils.validateEntity(mesBom);
        return Response.succeed(mesBomService.editMesBom(mesBom));
    }

    /**
     * 获取工位
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(mesBomService.getMesBom(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        mesBomService.del(ids);
        return Response.succeed();
    }

    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        mesBomService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        mesBomService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 导入
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/import")
    @ResponseBody
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile, HttpServletResponse response) throws IOException {
        return Response.succeed(mesBomService.importExcel(multipartFile,response));
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody MesBomPageParam mesBomPageParam) {
        return Response.succeed(mesBomService.findPageListByParam(mesBomPageParam));
    }
}
